/*
 * IPanel.java
 *
 * Created on 5. srpen 2006, 15:04
 */
package vision;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Date;

/**
 *
 * @author Kotuc
 */
class ImagePanel extends javax.swing.JPanel {

    private boolean autoresize = false;

    /** Creates new form IPanel */
    public ImagePanel() {
        initComponents();
    }
    public Image image;

    public void setAutoresize(boolean autoresize) {
        this.autoresize = autoresize;
    }

    public boolean isAutoresize() {
        return autoresize;
    }

    public void setImage(Image image) {
        this.image = image;
        setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        int width = getWidth(), height = getHeight();

        if (image == null) {
            g.drawString("" + image, width / 2, height / 2);
            g.drawString("" + new Date(System.currentTimeMillis()), width / 2, height / 2 + 20);
            return;
        }

        int iw = image.getWidth(null), ih = image.getHeight(null);

        if (autoresize) {
            if (((double) iw / ih) > ((double) width / height)) {
                g.drawImage(image, 0, 0, width, ih * width / iw, Color.RED, this);
            } else {
                g.drawImage(image, 0, 0, iw * height / ih, height, Color.RED, this);
            }
        } else {
            g.drawImage(image, 0, 0, this);
        }

//        if (false) {
//            if (((double)iw/ih)>((double)width/height)) {
//                g.drawImage(new ResizedImage((BufferedImage)image, width, ih*width/iw), 0, 0, null);
//            } else {
//                g.drawImage(new ResizedImage((BufferedImage)image, iw*height/ih, height), 0, 0, null);
//            }
//        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents
    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
//        JFrame f = new JFrame("nahled");
//        IPanel ip = new IPanel();
//        ip.setImage(image);
//        f.add(ip);
//        f.setSize(image.getWidth(this), image.getHeight(this));
    }//GEN-LAST:event_formMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
